<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="护理员" prop="nurseName">
        <el-input
          v-model="queryParams.nurseName"
          placeholder="请输入护理员姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="排班日期" prop="scheduleDate">
        <el-date-picker
          v-model="queryParams.scheduleDate"
          type="date"
          placeholder="选择排班日期"
          value-format="yyyy-MM-dd"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="班次" prop="shift">
        <el-select
          v-model="queryParams.shift"
          placeholder="请选择班次"
          clearable
        >
          <el-option
            v-for="dict in dict.type.nurse_schedule_shift"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['nursing:schedule:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['nursing:schedule:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['nursing:schedule:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['nursing:schedule:export']"
          >导出</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleBatchAdd"
          v-hasPermi="['nursing:schedule:add']"
          >批量排班</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="scheduleList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="排班ID"
        align="center"
        prop="scheduleId"
        v-if="false"
      />
      <el-table-column label="护理员姓名" align="center" prop="nurseName" />
      <el-table-column label="护理员工号" align="center" prop="nurseNumber" />
      <el-table-column
        label="排班日期"
        align="center"
        prop="scheduleDate"
        width="100"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.scheduleDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="班次" align="center" prop="shift">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.nurse_schedule_shift"
            :value="scope.row.shift"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="开始时间"
        align="center"
        prop="startTime"
        width="100"
      />
      <el-table-column
        label="结束时间"
        align="center"
        prop="endTime"
        width="100"
      />
      <el-table-column label="工作区域" align="center" prop="workArea" />
      <el-table-column
        label="备注"
        align="center"
        prop="remark"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="160"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['nursing:schedule:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['nursing:schedule:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改排班对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="护理员" prop="nurseId">
          <el-select
            v-model="form.nurseId"
            placeholder="请选择护理员"
            filterable
          >
            <el-option
              v-for="item in nurseOptions"
              :key="item.nurseId"
              :label="item.nurseName"
              :value="item.nurseId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排班日期" prop="scheduleDate">
          <el-date-picker
            v-model="form.scheduleDate"
            type="date"
            placeholder="选择排班日期"
            value-format="yyyy-MM-dd"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="班次" prop="shift">
          <el-select v-model="form.shift" placeholder="请选择班次">
            <el-option
              v-for="dict in dict.type.nurse_schedule_shift"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-time-picker
            v-model="form.startTime"
            placeholder="选择开始时间"
            format="HH:mm"
            value-format="HH:mm"
          ></el-time-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-time-picker
            v-model="form.endTime"
            placeholder="选择结束时间"
            format="HH:mm"
            value-format="HH:mm"
          ></el-time-picker>
        </el-form-item>
        <el-form-item label="工作区域" prop="workArea">
          <el-input v-model="form.workArea" placeholder="请输入工作区域" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 批量排班对话框 -->
    <el-dialog
      :title="'批量排班'"
      :visible.sync="batchOpen"
      width="600px"
      append-to-body
    >
      <el-form
        ref="batchForm"
        :model="batchForm"
        :rules="batchRules"
        label-width="100px"
      >
        <el-form-item label="护理员" prop="nurseIds">
          <el-select
            v-model="batchForm.nurseIds"
            multiple
            placeholder="请选择护理员"
            filterable
          >
            <el-option
              v-for="item in nurseOptions"
              :key="item.nurseId"
              :label="item.nurseName"
              :value="item.nurseId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="日期范围" prop="dateRange">
          <el-date-picker
            v-model="batchForm.dateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="班次" prop="shift">
          <el-select v-model="batchForm.shift" placeholder="请选择班次">
            <el-option
              v-for="dict in dict.type.nurse_schedule_shift"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-time-picker
            v-model="batchForm.startTime"
            placeholder="选择开始时间"
            format="HH:mm"
            value-format="HH:mm"
          ></el-time-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-time-picker
            v-model="batchForm.endTime"
            placeholder="选择结束时间"
            format="HH:mm"
            value-format="HH:mm"
          ></el-time-picker>
        </el-form-item>
        <el-form-item label="工作区域" prop="workArea">
          <el-input v-model="batchForm.workArea" placeholder="请输入工作区域" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="batchForm.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitBatchForm">确 定</el-button>
        <el-button @click="batchOpen = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listNurseSchedule,
  getNurseSchedule,
  addNurseSchedule,
  updateNurseSchedule,
  deleteNurseSchedule,
  exportNurseSchedule,
  batchAddNurseSchedule,
} from "@/api/system/nurseSchedule";
import { listNurse } from "@/api/system/nurse";

export default {
  name: "NurseSchedule",
  dicts: ["nurse_schedule_shift"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 护理员排班表格数据
      scheduleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示批量排班弹出层
      batchOpen: false,
      // 护理员选项
      nurseOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nurseName: null,
        scheduleDate: null,
        shift: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        nurseId: [
          { required: true, message: "护理员不能为空", trigger: "blur" },
        ],
        scheduleDate: [
          { required: true, message: "排班日期不能为空", trigger: "blur" },
        ],
        shift: [{ required: true, message: "班次不能为空", trigger: "change" }],
        startTime: [
          { required: true, message: "开始时间不能为空", trigger: "blur" },
        ],
        endTime: [
          { required: true, message: "结束时间不能为空", trigger: "blur" },
        ],
      },
      // 批量表单参数
      batchForm: {
        nurseIds: [],
        dateRange: [],
        shift: undefined,
        startTime: "",
        endTime: "",
        workArea: "",
        remark: "",
      },
      // 批量表单校验
      batchRules: {
        nurseIds: [
          { required: true, message: "护理员不能为空", trigger: "blur" },
        ],
        dateRange: [
          { required: true, message: "日期范围不能为空", trigger: "blur" },
        ],
        shift: [{ required: true, message: "班次不能为空", trigger: "change" }],
        startTime: [
          { required: true, message: "开始时间不能为空", trigger: "blur" },
        ],
        endTime: [
          { required: true, message: "结束时间不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getNurseOptions();
  },
  methods: {
    /** 查询护理员排班列表 */
    getList() {
      this.loading = true;
      listNurseSchedule(this.queryParams).then((response) => {
        this.scheduleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询护理员选项 */
    getNurseOptions() {
      listNurse().then((response) => {
        this.nurseOptions = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        scheduleId: null,
        nurseId: null,
        scheduleDate: null,
        shift: undefined,
        startTime: "",
        endTime: "",
        workArea: "",
        remark: "",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.scheduleId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加护理员排班";
    },
    /** 批量排班按钮操作 */
    handleBatchAdd() {
      this.batchForm = {
        nurseIds: [],
        dateRange: [],
        shift: undefined,
        startTime: "",
        endTime: "",
        workArea: "",
        remark: "",
      };
      this.resetForm("batchForm");
      this.batchOpen = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const scheduleId = row.scheduleId || this.ids;
      getNurseSchedule(scheduleId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改护理员排班";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.scheduleId != null) {
            updateNurseSchedule(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addNurseSchedule(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 提交批量排班 */
    submitBatchForm() {
      this.$refs["batchForm"].validate((valid) => {
        if (valid) {
          batchAddNurseSchedule(this.batchForm).then((response) => {
            this.$modal.msgSuccess("批量排班成功");
            this.batchOpen = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const scheduleIds = row.scheduleId || this.ids;
      this.$modal
        .confirm('是否确认删除护理员排班编号为"' + scheduleIds + '"的数据项?')
        .then(() => {
          return deleteNurseSchedule(scheduleIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.$modal
        .confirm("是否确认导出所有护理员排班数据项?")
        .then(() => {
          this.exportLoading = true;
          return exportNurseSchedule(this.queryParams);
        })
        .then((response) => {
          this.$download.excel(response, "护理员排班数据");
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>
